import Vue from "vue";
import VueRouter from "vue-router";

const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return routerPush.call(this, location).catch((err) => { });
};
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "layout",
    meta: {
      name: "首页",
    },
    component: () => import("@/views/Layout/index.vue"),
    redirect: '/home',
    children: [
      {
        path: "/home",
        name: "home",
        meta: {
          name: "首页",
        },
        // component: () => import("@/views/Notice/index.vue"),
        component: () => import("@/views/home/index.vue"),

      },
      {
        path: "/notice",
        name: "notice",
        meta: {
          name: "通知管理",
        },
        // component: () => import("@/views/Notice/index.vue"),
        component: () => import("@/views/Notice/adminIndex.vue"),

      },
      {
        path: "/noticeUser",
        name: "noticeUser",
        meta: {
          name: "通知管理",
        },
        component: () => import("@/views/Notice/index.vue"),
        // component: () => import("@/views/Notice/adminIndex.vue"),
      },
      {
        path: "/playerManage",
        name: "playerManage",
        meta: {
          name: "球员管理",
        },
        component: () => import("@/views/playerManage/index.vue"),

      },
      {
        path: "/raceManage",
        name: "raceManage",
        meta: {
          name: "比赛管理",
        },
        component: () => import("@/views/raceManage/index.vue"),

      },
      {
        path: "/trainManage",
        name: "trainManage",
        meta: {
          name: "训练管理",
        },
        component: () => import("@/views/trainManage/index.vue"),

      },
      {
        path: '/dManage',
        name: "dManage",
        meta: {
          name: '日程管理'
        },
        component: () => import("@/views/dateManage/index.vue"),
      },
      {
        path: '/permissionManage',
        name: "permissionManage",
        meta: {
          name: '权限管理'
        },
        component: () => import("@/views/permissionManage/index.vue"),
      },
      {
        path: '/roleManage',
        name: "roleManage",
        component: () => import("@/views/roleManage/index.vue")
      },
      {
        path: '/info',
        name: "info",
        component: () => import("@/views/info/index.vue")
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("@/views/Login/index.vue"),
  },
];
const stactRoutes = [
  {
    path: "/home",
    name: "home",
    meta: {
      name: "首页",
    },
    // component: () => import("@/views/Notice/index.vue"),
    component: () => import("@/views/home/index.vue"),

  },
  {
    path: "/notice",
    name: "notice",
    meta: {
      name: "通知管理",
    },
    // component: () => import("@/views/Notice/index.vue"),
    component: () => import("@/views/Notice/adminIndex.vue"),

  },
  {
    path: "/noticeUser",
    name: "noticeUser",
    meta: {
      name: "通知管理",
    },
    component: () => import("@/views/Notice/index.vue"),
    // component: () => import("@/views/Notice/adminIndex.vue"),
  },
  {
    path: "/playerManage",
    name: "playerManage",
    meta: {
      name: "球员管理",
    },
    component: () => import("@/views/playerManage/index.vue"),

  },
  {
    path: "/raceManage",
    name: "raceManage",
    meta: {
      name: "比赛管理",
    },
    component: () => import("@/views/raceManage/index.vue"),

  },
  {
    path: "/trainManage",
    name: "trainManage",
    meta: {
      name: "训练管理",
    },
    component: () => import("@/views/trainManage/index.vue"),

  },
  {
    path: '/dManage',
    name: "dManage",
    meta: {
      name: '日程管理'
    },
    component: () => import("@/views/dateManage/index.vue"),
  },
  {
    path: '/permissionManage',
    name: "permissionManage",
    meta: {
      name: '权限管理'
    },
    component: () => import("@/views/permissionManage/index.vue"),
  },
  {
    path: '/roleManage',
    name: "roleManage",
    component: () => import("@/views/roleManage/index.vue")
  },
  {
    path: '/info',
    name: "info",
    component: () => import("@/views/info/index.vue")
  },
]
console.log(JSON.parse(localStorage.getItem('menu')));
const arr = [
  {
    path: "/",
    name: "layout",
    meta: {
      name: "首页",
    },
    component: () => import("@/views/Layout/index.vue"),
    redirect: '/login',
    children: [
      {
        path: '/info',
        name: "info",
        component: () => import("@/views/info/index.vue")
      },
      {
        path: "/noticeUser",
        name: "noticeUser",
        meta: {
          name: "通知管理",
        },
        component: () => import("@/views/Notice/index.vue"),
        // component: () => import("@/views/Notice/adminIndex.vue"),
      },
      
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("@/views/Login/index.vue"),
  },
]
let menu = JSON.parse(localStorage.getItem('menu')) || []
menu.forEach(item => {
  stactRoutes.forEach(_item => {
    if (_item.name == item) {
      arr[0].children.push(_item)
    }
  })
})

console.log(arr,'ar1r');

const router = new VueRouter({
  routes:arr,
});
// router.addRoute('layout', playerManageRoute)
// JSON.parse(localStorage.getItem('menu')).forEach(item => {
//   routeArr.forEach(_item => {
//     if (_item.path.replace('/', '') == item) {
//       console.log(_item,'op');

//       router.addRoute('layout', _item)
//     }
//   })
// })

export default router;
